package com.betterwood.dragon.controllers;

import java.util.List;
import com.betterwood.dragon.core.common.JsonResult;
import com.betterwood.dragon.entity.Product;
import com.betterwood.dragon.entity.dto.ProductUpdate;
import com.betterwood.dragon.service.ProductService;
import com.betterwood.dragon.utils.RedisUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value = "/product")
@CrossOrigin
// 使用Lombok自动生成注入的构造函数
@RequiredArgsConstructor
@Slf4j
public class ProductController {

    private final ProductService productService;
    private final RedisUtil redisUtil;

    // private final RedisTemplate<String, String> template;

    // @Resource(name = "redisTemplate")
    // private ListOperations<String, String> listOps;

    @PostMapping(value = "/updateStatus")
    public JsonResult<Product> updateStatus(@RequestBody ProductUpdate pUpdate) {
        Product p = productService.findById(pUpdate.getId());
        p.setStatus(pUpdate.getStatus());
        return JsonResult.success(productService.save(p));
    }

    @PostMapping(value = "/listProducts")
    public JsonResult<List<Product>> listProduct(@RequestBody Product query) {
        // log.info(query);
        return JsonResult.success(productService.listByQuery(query)); // 因为已经做了字段验证,所以几乎没有错误可能性, 直接存了。

    }

    // @PostMapping(value = "/flipStatus")
    // public JsonResult<List<Product>> (@RequestBody Product query)
    // {
    // log.info(query);
    // return JsonResult.success(productService.listByQuery(query)); //
    // 因为已经做了字段验证,所以几乎没有错误可能性, 直接存了。

    // }

    @PostMapping(value = "/flipStatus")
    public JsonResult<String> flipStatus(@RequestBody Product query) {
        // log.info(query);
        productService.flipStatus(query.getId(), query.getStatus());
        return JsonResult.success("ok"); // 因为已经做了字段验证,所以几乎没有错误可能性, 直接存了。
    }

}
